package com.example.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.demo.po.TestPo;
import com.example.demo.po.UserManage;
import com.example.demo.po.WxUserInfo;
import com.example.demo.service.UserManagerService;
import com.example.demo.service.WxUserInfoService;
import com.example.demo.util.DateUtil;
import com.example.demo.util.WxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/lg")
public class LoginController {

    @Autowired
    private UserManagerService userService;
    @Autowired
    private WxUserInfoService wxUserInfoService;


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String GetUser(@RequestBody TestPo testPo, HttpServletRequest request, HttpSession session) {
        System.out.println(testPo.getUsername());
        UserManage userInfo = userService.getByUserName(testPo.getUsername());
        System.out.println(userInfo.getUserPwd());
        if(userInfo!=null&&(userInfo.getUserPwd()!=null&&!userInfo.getUserPwd().trim().equals(""))){
            String newMd5 = DigestUtils.md5DigestAsHex(testPo.getUserpwd().getBytes());
            if(newMd5.equals(userInfo.getUserPwd())) {
                request.getSession().setAttribute("session_user",userInfo);
                System.out.println("....................");
                return "success";
            }else {
                System.out.println("*******************");
                return "false";
            }
        }else
            return "false";

    }

    @RequestMapping(value = "/appLogin",method = RequestMethod.POST)
    public String appLogin(@RequestParam Map<String,String> map, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            System.out.println(map.toString());
            System.out.println("openid is "+map.get("openid"));
            String Skey = map.get("skey");
            map.remove("skey");
            String iv = map.get("iv");
            map.remove("iv");
            String eData = map.get("eData");
            map.remove("eData");
            String mphoto = WxUtil.decryptS5(eData,"UTF-8",Skey,iv);
            System.out.println(mphoto);
            WxUserInfo wxUserInfo =JSONObject.parseObject(JSONObject.toJSONString(map), WxUserInfo.class);
            wxUserInfo.setMphono(mphoto);
            wxUserInfo.setCzTime(DateUtil.formatDate(new Date()));
            wxUserInfo.setUserType(1);
            if(wxUserInfoService.saveOrUpdate(wxUserInfo)){
                String sessionId =session.getId();
                jsonObject.put("oper_res",true);
                jsonObject.put("data","登录成功!");
                jsonObject.put("sessionId",sessionId);
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","登录失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","登录错误!");
        }
        return jsonObject.toJSONString();
    }



    @RequestMapping(value = "/index" ,method = RequestMethod.GET)
    public ModelAndView testReturn(HttpServletRequest request, HttpServletResponse response){
        ModelAndView model = new ModelAndView();
        System.out.println("进入跳转首页");
        model.setViewName("index");
        return model;
    }

}
